﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using Microsoft.Office.Interop.Outlook;
using Exception = System.Exception;

namespace StatusManager
{
    class Helper
    {
        /// <summary>
        /// This method parse settings from Xml
        /// </summary>
        /// <param name="filename">Path to Xml with settings</param>
        /// <returns></returns>
        public static Dictionary<string, string> ParseCredentialsFromXml(string filename)
        {
            var result = new Dictionary<string, string>();
           
            var xml = XDocument.Load(filename);
            try
            {
                result = xml.Descendants("item")
                  .ToDictionary(d => (string)d.Attribute("name"),
                                d => (string)d);
            }
            catch (Exception e)
            {
                throw e;
            }

            return result;
        }

        public static void CreateEmail(string outlookLogin, string outlookPassword)
        {
            Application outLookApp = null;

            // Check whether there is an Outlook process running.
            int outlookRunning = Process.GetProcessesByName("OUTLOOK").Length;
            if (outlookRunning > 0)
            {
                try
                {
                    outLookApp =
                        Marshal.GetActiveObject("Outlook.Application") as Application;
                }
                catch (Exception)
                {
                    outLookApp =
                        Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application")) as
                            Application;
                }
                finally
                {
                    // At this point we must kill Outlook (since outlook was started by user on a higher prio level than this current application)
                    // kill Outlook (otherwise it will only work if UAC is disabled)
                    // this is really a kind of last resort
                    Process[] workers = Process.GetProcessesByName("OUTLOOk");
                    foreach (Process worker in workers)
                    {
                        worker.Kill();
                        worker.WaitForExit();
                        worker.Dispose();
                    }
                }
            }
            else
            {
                outLookApp = new Application();
                NameSpace nameSpace = outLookApp.GetNamespace("MAPI");
                try
                {
                    // use default profile and DO NOT pop up a window
                    // on some pc bill gates fails to login without the popup, then we must pop up and lets use choose profile and allow access
                    nameSpace.Logon(outlookLogin, outlookPassword, false, Missing.Value);
                }
                catch (Exception)
                {
                    // use default profile and DO pop up a window
                    nameSpace.Logon(outlookLogin, outlookPassword, true, true);
                }
                nameSpace = null;
            }

            // Create a new mail item.
            MailItem oMsg = (MailItem)outLookApp.CreateItem(OlItemType.olMailItem);

            oMsg.Body = "Test";

        }


    }
}
